home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13165 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: mail2news.demon.co.uk!txwang
  2. From: Wang TianXing <gztxwang@public1.guangzhou.gd.cn>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: object creation from an abstract base class
  5. Date: Sun, 24 Mar 1996 10:54:23 GMT
  6. Message-ID: <199603232309.HAA23055@public1.guangzhou.gd.cn>
  7. X-NNTP-Posting-Host: txwang
  8. X-Newsreader: Forte Free Agent 1.0.82
  9. X-Mail2News-Path: public1.guangzhou.gd.cn!txwang
  10.  
  11. On 23 Mar 1996 18:22:26 GMT, grantp@usa.pipeline.com(Pete Grant)
  12. wrote:
  13.  
  14. | On Mar 21, 1996 08:59:50 in article <Re: object creation from an abstract
  15. | base class>, 'Sukanta Ganguly <sukanta_ganguly@novell.com>' wrote: 
  16. |  
  17. |  
  18. | >Michael Catello wrote: 
  19. | >>  
  20. | >> Hello OOPsters, 
  21. | >>  
  22. | >> I was just looking for validation/other suggestions for a method I 
  23. | >> recently used in a program. I have defined an abstract base class 
  24. | >> (i.e. contains pure virtual functions), all access to the derived 
  25. | >> classes of this base are thru a pointer to the base class. To create 
  26. | >> the actual objects of the derived classes I used the following scheme: 
  27. | >>  
  28. | >> enum FooType {BAR, BAS}; 
  29. | >>  
  30. | >> // base class 
  31. | >> class CFoo 
  32. | >>         { 
  33. | >>         CFoo(); 
  34. | >>         ~CFoo(); 
  35. | >>  
  36. | >>         static CFoo* CreateFoo(FooType type); 
  37. | >>  
  38. | >>         // other methods/data including pure virtual fns whose behaviour
  39. |  
  40. | >will 
  41. | >> be defined in the derived classes 
  42. | >>         }; 
  43. | >>  
  44. | >> class CBar: public CFoo 
  45. | >>         { 
  46. | >>         // 
  47. | >>         }; 
  48. | >>  
  49. | >> class CBas: public CFoo 
  50. | >>         { 
  51. | >>         // 
  52. | >>         }; 
  53. | >>  
  54. | >> CFoo* CFoo::CreateFoo(FooType type) 
  55. | >>         { 
  56. | >>         CFoo* pfoo = NULL; 
  57. | >>  
  58. | >>         switch (type) 
  59. | >>                 { 
  60. | >>                 case BAR: 
  61. | >>                         pfoo = new CBar; 
  62. | >>                         break; 
  63. | >>                 case BAS: 
  64. | >>                         pfoo = new CBas; 
  65. | >>                         break; 
  66. | >>                 } 
  67. | >>  
  68. | >>         return pfoo; 
  69. | >>         } 
  70. | >>  
  71. | >> main() 
  72. | >>         { 
  73. | >>         CFoo* interface = CFoo::CreateFoo(BAR); 
  74. | >>         } 
  75. | >>  
  76. | >> Obviously it is the CreateFoo() function that I am wondering about. In 
  77. | >> the actual implementation I had multiple static "Create" functions for 
  78. | >> the base class that would allow me to create a new object: one based 
  79. | >> on an enumerated token (shown above), another an existing object, as 
  80. | >> well as one based on the format of a datafile. My application never 
  81. | >> references any of the derived classes directly, except in their 
  82. | >> creation and definition. 
  83. | >>  
  84. | >> Is there another/better/more appropriate way to handle this type of 
  85. | >> object creation? Thanks for your assistance, 
  86. | >>  
  87. | > [incorrect things deleted]
  88. |  
  89. | BTW, the statement "pfoo = new CBas;" is perfectly legal, 
  90. | and even proper.  Any compiler that "cribs" about it is wrong. 
  91.  
  92. So, the original poster should write code like this:
  93.  
  94.     CFoo *interface = new CBar;
  95.  
  96. and remove the static function CFoo::CreateFoo(FooType);
  97.  
  98. ---
  99. Wang TianXing
  100.  
  101.  
  102.